package com.example.my.meila.view.fragment.beautychildfrg;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.bumptech.glide.Glide;
import com.example.my.meila.R;
import com.example.my.meila.api.ExceptionHandle;
import com.example.my.meila.constast.Constast;
import com.example.my.meila.entity.Good;
import com.example.my.meila.presenter.GoodPresenter;
import com.example.my.meila.view.adapter.beautyadapter.GoodAdapter;
import com.example.my.meila.view.adapter.beautyadapter.GoodHeadAdapter;
import com.example.my.meila.view.impl.GoodView;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class GoodFragment extends Fragment implements GoodView {
    @BindView(R.id.good_xrv)
    XRecyclerView goodXrv;





    //    @BindView(R.id.item_good_rv)
    RecyclerView itemGoodRv;

    private GoodHeadAdapter adapter2;
    private HeadViewHolder headViewHolder;


    private List<Good.DataBean.BannersBean> banners = new ArrayList<>();

    private List<Good.DataBean.BurnFeedBean> data2 = new ArrayList<>();


    private GoodAdapter adapter;
    private List<Good.DataBean.FeedsBean> data = new ArrayList<>();
    private GoodPresenter presenter;

    public static GoodFragment newInstance() {

        Bundle args = new Bundle();

        GoodFragment fragment = new GoodFragment();
        fragment.setArguments(args);
        return fragment;
    }


    public GoodFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_good, container, false);
        ButterKnife.bind(this, view);

        initData();
        initView();


        return view;
    }




    @Override
    public void onPause() {
        super.onPause();
        headViewHolder.banner.stopTurning();
    }

    @Override
    public void onResume() {
        super.onResume();
        headViewHolder.banner.startTurning(2500);
    }


    private void initData() {
        presenter = new GoodPresenter(this);
        presenter.getGoodData();

    }

    private void initView() {


        this.goodXrv.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        this.goodXrv.setAdapter(adapter = new GoodAdapter(getContext(), data));


        headViewHolder = new HeadViewHolder();
        this.goodXrv.addHeaderView(headViewHolder.view);

        View view3=LayoutInflater.from(getContext()).inflate(R.layout.item_good_head3,null);
        AbsListView.LayoutParams params = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 80);
        view3.setLayoutParams(params);
        this.goodXrv.addHeaderView(view3);


        View view2 = LayoutInflater.from(getContext()).inflate(R.layout.item_beauty_good_rv, null);
//            ButterKnife.bind(this,view2);
        itemGoodRv= (RecyclerView) view2.findViewById(R.id.item_good_rv);
        this.goodXrv.addHeaderView(view2);
        this.itemGoodRv.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.HORIZONTAL,false));
        this.itemGoodRv.setAdapter(adapter2=new GoodHeadAdapter(getContext(),data2));




    }

    @Override
    public void onSuccess(Good good) {
        if (null != good) {
            data = good.getData().getFeeds();
            adapter.notifyDataSetChanged();


            banners.clear();
            banners.addAll(good.getData().getBanners());
            headViewHolder.banner.notifyDataSetChanged();


            data2 = good.getData().getBurn_feed();
            adapter2.notifyDataSetChanged();







        }


    }

    @Override
    public void onFail(ExceptionHandle.ResponeThrowable e) {

    }

    @Override
    public void onFinish() {

    }

    public class HeadViewHolder implements OnItemClickListener, CBViewHolderCreator<HeadViewHolder.ConvenientHolder> {
        @BindView(R.id.recomment_head_banner)
        ConvenientBanner<Good.DataBean.BannersBean> banner;
        View view;

        public HeadViewHolder() {
            view = LayoutInflater.from(getContext()).inflate(R.layout.recomment_item_head, null);

            ButterKnife.bind(this, view);
            banner.setPages(this, banners)
                    .setPageIndicator(new int[]{R.drawable.shape_dot_normal, R.drawable.shape_dot_sel})
                    .setOnItemClickListener(this)
                    .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL)
                    .setManualPageable(true);
        }

        @Override
        public ConvenientHolder createHolder() {
            return new ConvenientHolder();
        }

        @Override
        public void onItemClick(int position) {

        }

        public class ConvenientHolder implements Holder<Good.DataBean.BannersBean> {

            ImageView img;

            @Override
            public View createView(Context context) {
                img = new ImageView(context);
                img.setScaleType(ImageView.ScaleType.CENTER_CROP);
                return img;
            }

            @Override
            public void UpdateUI(Context context, int position, Good.DataBean.BannersBean data) {
                Glide.with(context).load(Constast.PUBLIC_URL + data.getImg()).placeholder(R.mipmap.ic_launcher).into(img);
            }
        }
    }

}

